{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# #第八章 向量代数与空间解析几何（高数下1-2页）\n",
    "在平面解析几何中,通过坐标法把平面上的点与一对有次序的数对应起来，把平面上的图形和方程对应起来,从而可以用代数方法来研究几何问题.空间解析几何也是按照类似的方法建立起来的.\n",
    "正像平面解析几何的知识对学习一元函数微积分是不可缺少的一样,空间解析几何的知识对学习多元函数微积分也是必要的.\n",
    "本章先引进向量的概念,根据向量的线性运算建立空间坐标系,然后利用坐标讨论向量的运算,并介绍空间解析几何的有关内容.\n",
    "                        #第一节向量及其线性运算\n",
    "\n",
    "一、向量的概念\n",
    "客观世界中有这样一类量,它们既有大小,又有方向,例如位移、速度、加速度、力、力矩等等,这一类量叫做向量(或矢量).\n",
    "在数学上,常用一条有方向的线段,即有向线段来表示向量.有向线段的长度表示向量的大小,有向线段的方向表示向量的方向.以A为起点、B为终点的有向线段所表示的向量记作AB(图8-1).有时也用一个黑体字母a来表示向量,如a、b、c等.\n",
    "实际问题中,向量常用来表示物体的速度、加速度、力、力矩等.\n",
    "矢量是向量的一种特殊情况,它只考虑向量的方向,而不考虑大小.\n",
    "\n",
    "向量的大小叫做向量的模.向量AB、a和a的模依次记作|AB|、|a|、|v|、|F|.向量的大小就是向量所表示的有向线段的长度.向量的模是正数,即|a|≥0.当|a|=0时,称向量a为零向量,记作0或0.\n",
    "矢量是向量的一种特殊情况,它只考虑向量的方向,而不考虑大小.矢量是向量的一个特例,它只考虑向量的方向,而不考虑大小.\n",
    "二、向量的线性运算\n",
    "向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积等运算.向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积等运算.向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积等运算.向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积等运算.向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积等运算.向量的线性运算是向量最基本的运算,它决定了向量的加法和减法,向量的标量乘法,向量的点积,向量的叉积 \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'numpy'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msympy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01msp\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mIPython\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdisplay\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m display, Math\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'numpy'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import sympy as sp\n",
    "from IPython.display import display, Math\n",
    "\n",
    "# 向量的表示\n",
    "A = np.array([1, 2])\n",
    "B = np.array([3, 4])\n",
    "\n",
    "# 计算向量的模\n",
    "def vector_magnitude(vector):\n",
    "    \"\"\"\n",
    "    计算向量的模（长度）\n",
    "    \n",
    "    参数:\n",
    "    vector (numpy.ndarray): 输入向量\n",
    "    \n",
    "    返回:\n",
    "    float: 向量的模\n",
    "    \"\"\"\n",
    "    if not isinstance(vector, np.ndarray):\n",
    "        raise TypeError(\"Input must be a numpy array\")\n",
    "    return np.linalg.norm(vector)\n",
    "\n",
    "magnitude_A = vector_magnitude(A)\n",
    "magnitude_B = vector_magnitude(B)\n",
    "\n",
    "# 向量的加法\n",
    "sum_AB = A + B\n",
    "\n",
    "# 向量的减法\n",
    "diff_AB = A - B\n",
    "\n",
    "# 向量的点积\n",
    "dot_product = np.dot(A, B)\n",
    "\n",
    "import math\n",
    "\n",
    "# 计算两个向量之间的夹角\n",
    "def angle_between_vectors(a, b):\n",
    "    \"\"\"\n",
    "    计算两个向量之间的夹角（弧度）\n",
    "    \n",
    "    参数:\n",
    "    a (numpy.ndarray): 第一个向量\n",
    "    b (numpy.ndarray): 第二个向量\n",
    "    \n",
    "    返回:\n",
    "    float: 两个向量之间的夹角（弧度）\n",
    "    \"\"\"\n",
    "    if not (isinstance(a, np.ndarray) and isinstance(b, np.ndarray)):\n",
    "        raise TypeError(\"Inputs must be numpy arrays\")\n",
    "    dot_prod = np.dot(a, b)\n",
    "    mag_a = vector_magnitude(a)\n",
    "    mag_b = vector_magnitude(b)\n",
    "    if mag_a == 0 or mag_b == 0:\n",
    "        return np.nan\n",
    "    return math.acos(dot_prod / (mag_a * mag_b))\n",
    "\n",
    "angle_AB = angle_between_vectors(A, B)\n",
    "\n",
    "a, b = sp.symbols('a b')\n",
    "display(Math(r'\\vec{a} + \\vec{b} = \\vec{c}'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.向量的加法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "  \n",
    "a = np.array([1, 2])\n",
    "b = np.array([3, 4])\n",
    "c = a + b\n",
    "display(Math(r'\\vec{a} + \\vec{b} = \\vec{c}'))\n",
    "display(Math(r'\\vec{a} = ({}, {})'.format(a[0], a[1])))\n",
    "display(Math(r'\\vec{b} = ({}, {})'.format(b[0], b[1])))\n",
    "display(Math(r'\\vec{c} = ({}, {})'.format(c[0], c[1])))\n",
    " \n",
    " \n",
    "这里我们定义了两个向量 a 和 b ，并计算了它们的和 c ，然后使用 display(Math()) 来显示向量加法的公式和结果。\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.向量的交换律和结合律"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交换律\n",
    "a = np.array([1, 2])\n",
    "b = np.array([3, 4])\n",
    "sum1 = a + b\n",
    "sum2 = b + a\n",
    "display(Math(r'(\\vec{a} + \\vec{b}) = (\\vec{b} + \\vec{a})'))\n",
    "display(Math(r'{} = {}'.format(sum1, sum2)))\n",
    "\n",
    "# 结合律\n",
    "a = np.array([1, 2])\n",
    "b = np.array([3, 4])\n",
    "c = np.array([5, 6])\n",
    "sum3 = (a + b) + c\n",
    "sum4 = a + (b + c)\n",
    "display(Math(r'((\\vec{a} + \\vec{b}) + \\vec{c}) = (\\vec{a} + (\\vec{b} + \\vec{c}))'))\n",
    "display(Math(r'{} = {}'.format(sum3, sum4)))\n",
    " \n",
    " \n",
    "这里我们分别定义了向量 a 、 b 和 c ，并验证了向量加法的交换律和结合律。\n",
    " \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.向量的减法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "  a = np.array([3, 4])\n",
    "b = np.array([1, 2])\n",
    "diff = a - b\n",
    "display(Math(r'\\vec{a} - \\vec{b} = \\vec{d}'))\n",
    "display(Math(r'\\vec{a} = ({}, {})'.format(a[0], a[1])))\n",
    "display(Math(r'\\vec{b} = ({}, {})'.format(b[0], b[1])))\n",
    "display(Math(r'\\vec{d} = ({}, {})'.format(diff[0], diff[1])))\n",
    " \n",
    " \n",
    "这里我们定义了两个向量 a 和 b ，并计算了它们的差 diff ，然后使用 display(Math()) 来显示向量减法的公式和结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.向量的负向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "  \n",
    "a = np.array([1, 2])\n",
    "neg_a = -a\n",
    "display(Math(r'-\\vec{a} = (-{}, -{})'.format(a[0], a[1])))\n",
    "display(Math(r'-\\vec{a} = ({}, {})'.format(neg_a[0], neg_a[1])))\n",
    " \n",
    " \n",
    "这里我们定义了向量 a ，并计算了它的负向量 neg_a ，然后使用 display(Math()) 来显示负向量的公式和结果。\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5.向量与数的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from IPython.display import display, Math\n",
    " vector = np.array([2, 3])\n",
    "scalar = 4\n",
    " \n",
    "result_vector = scalar * vector\n",
    "display(Math(r'\\lambda \\vec{v} = \\vec{u}'))\n",
    "display(Math(r'\\lambda = {}, \\vec{v} = ({}, {})'.format(scalar, vector[0], vector[1])))\n",
    "display(Math(r'\\vec{u} = ({}, {})'.format(result_vector[0], result_vector[1])))\n",
    " \n",
    " \n",
    "这段代码先执行了向量与数的乘法运算（在Python中使用 * 实现），得到新的向量 result_vector ，然后利用 display 和 Math 函数展示出向量与数乘法对应的数量乘法运算。\n",
    " \n",
    "# 再定义另一个数（标量）\n",
    "scalar2 = 3\n",
    "left_side = (scalar + scalar2) * vector\n",
    "right_side = scalar * vector + scalar2 * vector\n",
    "display(Math(r'(\\lambda + \\mu) \\vec{v} = \\lambda \\vec{v} + \\mu \\vec{v}'))\n",
    "display(Math(r'(\\lambda + \\mu) = {}, \\vec{v} = ({}, {})'.format(scalar + scalar2, vector[0], vector[1])))\n",
    "display(Math(r'(\\lambda \\vec{v} + \\mu \\vec{v}) = ({}, {}) + ({}, {})'.format(scalar * vector[0], scalar * vector[1], scalar2 * vector[0], scalar2 * vector[1])))\n",
    "display(Math(r'左式向量：({}, {})，右式向量：({}, {})'.format(left_side[0], left_side[1], right_side[0], right_side[1])))\n",
    " \n",
    "\n",
    "这段代码先执行了向量与数的乘法运算（在Python中使用 * 实现），得到新的向量 result_vector ，然后利用 display 和 Math 函数展示出向量与数乘法对应的数量乘法运算。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量与数的乘积符合下列运算规律:(1)结合律λ(μa)=μ(λa)=(λμ)a.\n",
    "这是因为由向量与数的乘积的规定可知,向量λ(μa)、μ(λa)、(λμ)a 都是平行的向量,它们的方向也是相同的,而且\n",
    "|λ(μa)|=|μ(λa)|=|(λu)a|=|λμ||a1,\n",
    "所以\n",
    "λ(μa) =μ(λa)=(λp)a.\n",
    "(2)分配律\n",
    "(λ+μ)a=λa +μa,\t\t\n",
    "λ(a+b) =λa +λb.\t\n",
    "这个规律同样可以按向量与数的乘积的规定来证明\n",
    "\n",
    "\n",
    "1. 向量与数的乘积的结合律：\n",
    "向量与数的乘积的结合律可以表示为：\n",
    "向量与数的乘积的结合律可以表示为：\n",
    "λ(μa) = μ(λa) = (λμ)a\n",
    "其中，λ和μ是标量，a是向量。这个规律表明，无论先乘以哪个标量，结果都是相同的。\n",
    "\n",
    "2. 向量与数的乘积的分配律：\n",
    "向量与数的乘积的分配律可以表示为：\n",
    "(λ+μ)a = λa + μa\n",
    "向量与数的乘积的分配律可以表示为：\n",
    "(λ+μ)a = λa + μa\n",
    "其中，λ和μ是标量，a是向量。这个规律表明，无论先乘以哪个标量，结果都是相同的。\n",
    "向量与数的乘积的分配律可以表示为：\n",
    "λ(a+b) = λa + λb\n",
    "向量与数的乘积的分配律可以表示为：\n",
    "λ(a+b) = λa + λb\n",
    "其中，λ是标量，a和b是向量。这个规律表明，无论先乘以哪个标量，结果都是相同的。\n",
    "向量与数的乘积的结合律和分配律是向量运算的基本性质，它们是向量运算的基本规则。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.空间直角坐标系"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空间直角坐标系\n",
    "在空间取定一点0和三个两两垂直的单位向量i、j、k,就确定了三条都以0为原点的两两垂直的数轴,依次记为x轴(横轴)、y轴(纵轴)、=轴(竖轴),统称坐标轴.它们构成一个空间直角坐标系,称为Oxyz坐标系或[0;i,j,k]坐标系(图8-10).通常把x轴和y轴配置在水平面上,而:轴则是铅垂线;它们的正向通常符合右手规则,即以右手握住z轴,当右手的四个手指从正向x轴以共角度转向正向y轴时,大拇指的指向就是z轴的正向\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
